package com.thinkTank.oa.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkTank.oa.base.BaseServiceImpl;
import com.thinkTank.oa.dao.ConferenceMapper;
import com.thinkTank.oa.dao.CustomerMapper;
import com.thinkTank.oa.dao.DemandMapper;
import com.thinkTank.oa.dao.DetailsMapper;
import com.thinkTank.oa.dao.PersonMapper;
import com.thinkTank.oa.dao.ProjectMapper;
import com.thinkTank.oa.entity.ButtDetails;
import com.thinkTank.oa.entity.Conference;
import com.thinkTank.oa.entity.Customer;
import com.thinkTank.oa.entity.Demand;
import com.thinkTank.oa.entity.Person;
import com.thinkTank.oa.entity.Project;
import com.thinkTank.oa.entity.User;
import com.thinkTank.oa.service.DetailsService;
import com.thinkTank.oa.service.UserService;
import com.thinkTank.oa.util.StrUtil;

/**
 * 对接详情serviceImpl
 * @author 董力 
 * 2018-08-20
 */
@Transactional
@Service("detailsService")
public class DetailsServiceImpl extends BaseServiceImpl<ButtDetails> implements DetailsService {

    @Autowired
    private DetailsMapper detailsMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private DemandMapper demandMapper;
    @Autowired
    private PersonMapper personMapper;
    @Autowired
    private CustomerMapper coustmoerMapper;
    @Autowired
    private ConferenceMapper  conferenceMapper;
    @Autowired
    private UserService userService;
    
    @Autowired
    public void setDetailsMapper(DetailsMapper detailsMapper) {
        this.detailsMapper = detailsMapper;
        super.setBaseMapper(detailsMapper);
    }
    
    /**
	 * 新增
	 * @param details
	 */
	public void addDetails(ButtDetails details){
		detailsMapper.addDetails(details);
	}
	
	/**
	 * 模糊查询对接详情表
	 * @param details
	 * @return
	 */
	public List<ButtDetails> findMDetailsList(ButtDetails details){
		return detailsMapper.findMDetailsList(details);
	}
	
	/**
	 * 通过pid查询对接详情
	 * @param id
	 * @return
	 */
	public ButtDetails findDetailsByPid(String pid){
		return detailsMapper.findDetailsByPid(pid);
	}
	
	
	/**
	 * 修改
	 * @param details
	 */
	public void updateDetails(ButtDetails details){
		detailsMapper.updateDetails(details);
	}

	@Override
	public Integer addFirstButtDetails(Project projectId, Demand demandId, Conference confId, Customer custromId,
			Person presonId,String userId) {
		ButtDetails  buttDetails = new ButtDetails();
		User user = userService.getById(userId);
		buttDetails.setButtName(user.getFullName());
		buttDetails.setButtPersonId(userId);
		buttDetails.setButtDetails("第一次匹配未联系!");
		String dateTime = StrUtil.getCurrentDateTime();
		buttDetails.setButtDataTime(dateTime);
		buttDetails.setAddDateTime(StrUtil.getCurrentDateTime_two());
		buttDetails.setFuture(1);
		buttDetails.setButtStatus(1);
		
		buttDetails.setPid(StrUtil.getUUID32());
		String remark="{";
		
		if(projectId!=null){
			buttDetails.setProjectNumber(projectId.getPid());
			remark+="projectId:'R"+projectId.getProjectNumber() +"',";
			remark+="projectName:'"+projectId.getProjectName() +"',";
		} 
		if(demandId!=null){
			buttDetails.setDemandNumber(demandId.getPid());
			remark+="demandId:'D"+demandId.getDemandNumber()+"',";
			remark+="demandName:'"+demandId.getDemandName()+"',";
		}
		if(confId!=null){
			buttDetails.setConferenceNumber(confId.getPid());
			remark+="confId:'A"+confId.getConferenceNumber() +"',";
			remark+="confTitle:'"+confId.getConferenceName()+"',";
		}
		if(custromId!=null){
			buttDetails.setCustomerNumber(custromId.getPid());
			remark+="customerId:'C"+custromId.getCustomerNumber() +"',";
			remark+="customerName:'"+custromId.getCustomerName()+"',";
		}
		if(presonId!=null){
			buttDetails.setExpertNumber(presonId.getPid());
			remark+="expertId:'E"+presonId.getExpertNumber()+"',";
			remark+="expertName:'"+presonId.getName()+"',";
		}
		remark=remark.substring(0, remark.length()-1);
		remark+="}";
		buttDetails.setRemark1(remark);
		detailsMapper.addDetails(buttDetails);
		return 1;
	}

	@Override
	public List<ButtDetails> findButtDetailsByUserId(String userId) {
		
		return detailsMapper.findButtDetailsByUserId(userId);
	}

	@Override
	public Integer addButtDetails(ButtDetails olderButt, ButtDetails buttDetails) {
		olderButt.setFuture(0);
		detailsMapper.updateDetails(olderButt);
		Project project = null;
		Demand demand = null;
		Conference conf = null;
		Customer custrom = null;
		Person preson  = null;
		String remark="{";
		if(buttDetails.getProjectNumber()!=null&&!"".equals(buttDetails.getProjectNumber())){
			 project = projectMapper.getById(buttDetails.getProjectNumber());
		}
		if(buttDetails.getDemandNumber()!=null&&!"".equals(buttDetails.getDemandNumber())){
						
			 demand =demandMapper.getById(buttDetails.getDemandNumber());
		}
		if(buttDetails.getExpertNumber()!=null&&!"".equals(buttDetails.getExpertNumber())){
			preson =personMapper.getById(buttDetails.getExpertNumber());
		}
		if(buttDetails.getCustomerNumber()!=null&&!"".equals(buttDetails.getCustomerNumber())){
			custrom =coustmoerMapper.getById(buttDetails.getCustomerNumber());
		}
		if(buttDetails.getConferenceNumber()!=null&&!"".equals(buttDetails.getConferenceNumber())){
			conf = conferenceMapper.getById(buttDetails.getConferenceNumber());
		}
		
		if(project!=null){
			
			remark+="projectId:'R"+project.getProjectNumber() +"',";
			remark+="projectName:'"+project.getProjectName() +"',";
		} 
		if(demand!=null){

			remark+="demandId:'D"+demand.getDemandNumber()+"',";
			remark+="demandName:'"+demand.getDemandName()+"',";
		}
		if(conf!=null){

			remark+="confId:'A"+conf.getConferenceNumber() +"',";
			remark+="confTitle:'"+conf.getConferenceName()+"',";
		}
		if(custrom!=null){

			remark+="customerId:'C"+custrom.getCustomerNumber() +"',";
			remark+="customerName:'"+custrom.getCustomerName()+"',";
		}
		if(preson!=null){

			remark+="expertId:'E"+preson.getExpertNumber()+"',";
			remark+="expertName:'"+preson.getName()+"',";
		}
		remark=remark.substring(0, remark.length()-1);
		remark+="}";
		buttDetails.setPid(StrUtil.getUUID32());
		buttDetails.setRemark1(remark);
		detailsMapper.addDetails(buttDetails);
		return 1;
	}

	@Override
	public List<ButtDetails> findHistoryButtDetails(ButtDetails olderButt) {
		return detailsMapper.findHistoryButtDetails( olderButt);
	}
	
	/**
	 * 通过专家模糊查询
	 * @author dongli
	 * @date 2018年8月24日
	 * @param personId
	 * @return
	 */
	public List<ButtDetails> findButtDetailsByPersonId(ButtDetails buttDetails){
		return detailsMapper.findButtDetailsByPersonId(buttDetails);
	}


	@Override
	public List<Map<Object,Object>> findButtDetailsGoupbyStatus(String userId) {
		return detailsMapper.findButtDetailsGoupbyStatus(userId);
		
	}

	@Override
	public List<ButtDetails> findButtDetailsByStatus(String userId, Integer status) {
		return detailsMapper.findButtDetailsByStatus(userId,status);
	}

	@Override
	public List<ButtDetails> findCustomerDemand(String customerId) {
		return detailsMapper.findCustomerDemand(customerId);
	}

	@Override
	public List<ButtDetails> findCustomerProject(String customerId) {
		return detailsMapper.findCustomerProject(customerId);
	}
	
	/**
	 * 通过专家pid查询对接详情
	 * @author dongli
	 * @date 2018年8月26日
	 * @param personId
	 * @return
	 */
	public ButtDetails getDetailsByPersonId(String personId){
		return detailsMapper.getDetailsByPersonId(personId);
	}

    @Override
    public List<ButtDetails> findDemandByCustomerId(ButtDetails buttDetails) {
        // TODO Auto-generated method stub
        return detailsMapper.findDemandByCustomerId(buttDetails);
    }

    @Override
    public List<ButtDetails> findProjectByCustomerId(ButtDetails buttDetails) {
        // TODO Auto-generated method stub
        return detailsMapper.findProjectByCustomerId(buttDetails);
    }


    	
	
    @Override
    public List<ButtDetails> findButtDetails(ButtDetails buttDetails) {
        return detailsMapper.findButtDetails(buttDetails);
    }
    
    /**
	 * 通过专家pid和需求pid来查询对接详情
	 * @author dongli
	 * @date 2018年9月27日
	 * @param buttDetails
	 * @return
	 */
	public List<ButtDetails> findDetailsByPersonIdAndDemandId(ButtDetails buttDetails){
		return detailsMapper.findDetailsByPersonIdAndDemandId(buttDetails);
	}

	@Override
	public List<ButtDetails> queryInButtDetails(ButtDetails buttDetails) {
		// TODO Auto-generated method stub
		return detailsMapper.queryInButtDetails(buttDetails);
	}
	
	/**
	 * 专家详情页面已匹配需求显示前10条数据
	 * @author dongli
	 * @date 2018年9月28日
	 * @param buttDetails
	 * @return
	 */
	public List<ButtDetails> findButtDetailsTop10(ButtDetails buttDetails){
		return detailsMapper.findButtDetailsTop10(buttDetails);
	}
	
	/**
	 * 通过专家pid联合查询需求信息
	 * @author dongli
	 * @date 2018年10月22日
	 * @param personId
	 * @return
	 */
	public List<Demand> queryDemandList(String personId){
		return detailsMapper.queryDemandList(personId);
	}
	

}
